<template>
  <el-dropdown
    class="international"
    trigger="click"
    @command="handleSetLanguage"
  >
    <div>
      <el-tooltip content="国际化" effect="dark">
        <svg-icon icon="language"></svg-icon>
      </el-tooltip>
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item :disabled="language === 'zh'" command="zh"
          >中文</el-dropdown-item
        >
        <el-dropdown-item :disabled="language === 'en'" command="en"
          >English</el-dropdown-item
        >
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup lang="ts">
import { computed, defineProps } from 'vue'
import { useAppStore } from '@/store'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'

defineProps({
  effect: {
    type: String,
    default: 'dark',
    validate: function (value) {
      return ['dark', 'light'].indexOf(value) !== -1
    }
  }
})

// // 获取语言配置
const appStore = useAppStore()
const language = computed(() => appStore.language)

// 切换语言
const i18n = useI18n()
const handleSetLanguage = (lang) => {
  i18n.locale.value = lang
  appStore.setLanguage(lang)
  ElMessage.success('切换成功')
}
</script>

<style lang="scss" scoped>
.international {
  font-size: 25px;
  cursor: pointer;
  ::v-deep {
    .svg-icon {
      fill: #fff;
      float: right;
    }
  }
}
</style>
